package com.hspedu.map_;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author Stringzhua
 * @Date 2024/3/12 20:55
 * description:演示map接口的常用方法
 */
@SuppressWarnings("all")
public class MapMethod {
    public static void main(String[] args) {
        //演示map接口的常用方法
        //1.put添加
        Map map = new HashMap();
        map.put("邓超", new Book("", 100));
        map.put("邓超", "孙俪");//替换-->一会查看源码
        map.put("王宝强", "马蓉");//ok
        map.put("宋喆", "马蓉");//ok
        map.put(null, "刘亦菲");//ok
        map.put("刘亦菲", null);//ok
        map.put("鹿晗", "关晓彤");//ok
        map.put("hsp","hsp的老婆");

        System.out.println("map = " + map);

        //2.remove：根据键删除映射关系
        map.remove(null);
        System.out.println("map = " + map);//k-v
        //3.get:根据键获取值
        Object value = map.get("鹿晗");
        System.out.println("value = " + value);
        //4.size:获取元素个数
        System.out.println("k-v= "+map.size());
        //5.isEmpty:判断个数是否为0
        System.out.println(map.isEmpty());//false
        //6.clear:清除,清空键值对
//        map.clear();
//        System.out.println("map = " + map);
        //7.containsKey:查找键是否存在
        System.out.println(map.containsKey("hsp"));//true
    }
}

class Book {
    private String name;
    private int num;

    public Book(String name, int num) {
        this.name = name;
        this.num = num;
    }
}